﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class ReviewRequest : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Roles.IsUserInRole("Operator"))
        {
            Response.Redirect("~/Account/Login.aspx");
        }
        string creditRequestId = null;
        if (Session["requestId"] != null)
        {
            creditRequestId = Session["requestId"].ToString();
        }
        if (creditRequestId != null)
        {
            var requests = from request in db.CreditRequests where request.CreditRequestId == Convert.ToInt32(creditRequestId) select request;
            string clientId = requests.First().UserProfileId.ToString();
            if (clientId != null)
            {
                var profiles = from profile in db.UserProfiles where profile.ProfileId == Convert.ToInt32(clientId) select profile;
                UserProfile clientProfile = profiles.First();
                CompanyName.Text = clientProfile.Company.CompanyName;
                CompanyName.Enabled = false;
                OrganizationForm.Enabled = false;
                HeadName.Text = clientProfile.CompanyHead;
                HeadName.Enabled = false;
                CompanyAddress.Text = clientProfile.Address;
                CompanyAddress.Enabled = false;
                CompanyPhone.Text = clientProfile.CompanyPhone;
                CompanyPhone.Enabled = false;
                CompanyEmail.Text = clientProfile.CompanyEmail;
                CompanyEmail.Enabled = false;
                CompanyProfit.Text = clientProfile.LastTwoYearsProfit.ToString();
                UserCommentAboutDate.Text = requests.First().UserComment;
                CompanyProfit.Enabled = false;
                HaveCredit.Checked = clientProfile.HasCredit;
                CompanySizeList.SelectedValue = clientProfile.Company.CompanySizeId.ToString();
                Activity.Text = clientProfile.Company.Activity.Name;
                CreationDate.Text = clientProfile.Company.CreationDate.ToShortDateString();
                HaveCredit.Enabled = false;
                CreditList.Enabled = false;
                TermLength.Text = requests.First().TermLength.ToString();
                TermLength.Enabled = false;
                Amount.Text = requests.First().Amount.ToString();
                Amount.Enabled = false;
                UserIsRegisterd.Enabled = false;
                CurrencyList.Enabled = false;
                if (clientProfile.userId == null)
                {
                    UserIsRegisterd.Checked = false;
                }
                else
                {
                    UserIsRegisterd.Checked = true;
                }
            }
        }
    }

    protected void CreditList_DataBound(object sender, EventArgs e)
    {
        string creditRequestId = null;
        if (Session["requestId"] != null)
        {
            creditRequestId = Session["requestId"].ToString();
        }
        if (creditRequestId != null)
        {
            var credit = from request in db.CreditRequests where request.CreditRequestId == Convert.ToInt32(creditRequestId) select request.Credit;
            string creditId = credit.First().CreditID.ToString();
            if (creditId != null && credit.First().Active)
            {
                CreditList.Items.FindByValue(creditId).Selected = true;
            }
        }
    }

    protected void OrganizationForm_DataBound(object sender, EventArgs e)
    {
        string creditRequestId = null;
        if (Session["requestId"] != null)
        {
            creditRequestId = Session["requestId"].ToString();
        }
        if (creditRequestId != null)
        {
            var requests = from request in db.CreditRequests where request.CreditRequestId == Convert.ToInt32(creditRequestId) select request;
            string clientId = requests.First().UserProfileId.ToString();
            if (clientId != null)
            {
                var profiles = from profile in db.UserProfiles where profile.ProfileId == Convert.ToInt32(clientId) select profile;
                UserProfile clientProfile = profiles.First();
                OrganizationForm.Items.FindByValue(clientProfile.Company.OrganitionalFormId.ToString()).Selected = true;
            }
        }
    }

    protected void CurrencyList_DataBound(object sender, EventArgs e)
    {
        string creditRequestId = null;
        if (Session["requestId"] != null)
        {
            creditRequestId = Session["requestId"].ToString();
        }
        if (creditRequestId != null)
        {
            var requests = from request in db.CreditRequests where request.CreditRequestId == Convert.ToInt32(creditRequestId) select request;
            CurrencyList.Items.FindByValue(requests.First().CurrencyId.ToString()).Selected = true;
        }
    }

    protected void ApproveRequestButton_Click(object sender, EventArgs e)
    {
        submitCreditRequest(1);
    }

    protected void RejectRequestButton_Click(object sender, EventArgs e)
    {
        submitCreditRequest(2);
    }

    protected int getOperatorId()
    {
        var operators = from bankOperator in db.Operators where bankOperator.UserId == (Guid)Membership.GetUser().ProviderUserKey select bankOperator;
        return operators.First().OperatorId;
    }

    protected void submitCreditRequest(int requestStatus)
    {
        string creditRequestId = null;
        if (Session["requestId"] != null)
        {
            creditRequestId = Session["requestId"].ToString();
        }
        if (creditRequestId != null)
        {
            var requests = from request in db.CreditRequests where request.CreditRequestId == Convert.ToInt32(creditRequestId) select request;
            CreditRequest creditRequest = requests.First();
            creditRequest.OperatorComments = OperatorComment.Text;
            creditRequest.OperatorId = getOperatorId();
            creditRequest.RequestStatusId = requestStatus;
            try
            {
                creditRequest.MeetingDate = DateTime.ParseExact(MeetingDate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
            }
            catch { }
            db.SubmitChanges();
            MailServer server = new MailServer();
            switch (requestStatus)
            {
                case 1:
                    server.sendMailAnoutApprovedRequest(creditRequest.UserProfile.CompanyEmail, creditRequest.Credit.Name,
                        creditRequest.Amount.ToString(), creditRequest.Currency.Name, creditRequest.TermLength.ToString(),
                        OperatorComment.Text, MeetingDate.Text);
                    break;
                case 2:
                    server.sendMailAboutRejectRequest(creditRequest.UserProfile.CompanyEmail, creditRequest.Credit.Name,
                        creditRequest.Amount.ToString(), creditRequest.Currency.Name, creditRequest.TermLength.ToString(),
                        OperatorComment.Text);
                    break;
            }
        }
        Session.Remove("requestId");
        Response.Redirect("~/Operator/Requests.aspx");
    }
}